// 登录表单提交逻辑
document.getElementById('login-form').addEventListener('submit', async (e) => {
    e.preventDefault();
    
    const username = document.getElementById('username').value.trim();
    const password = document.getElementById('password').value.trim();

    // 验证输入
    if (!username || !password) {
        alert('用户名和密码不能为空！');
        return;
    }

    // 从mock获取用户数据验证
    try {
        const response = await fetch('./mock/users.json');
        const users = await response.json();
        // 匹配用户名和密码
        const validUser = users.find(user => 
            user.username === username && user.password === password
        );

        if (validUser) {
            // 登录成功，跳转到仪表盘
            alert('登录成功！');
            window.location.href = 'dashboard.jsp';
        } else {
            alert('用户名或密码错误，请重新输入！');
        }
    } catch (error) {
        console.error('登录验证失败：', error);
        // 降级处理：默认admin/admin123登录
        if (username === 'admin' && password === 'admin123') {
            alert('登录成功！');
            window.location.href = 'dashboard.jsp';
        } else {
            alert('登录失败，请重试！');
        }
    }
});